﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Configuration;
using System.Data;
using MySQLStudio.DataAccess;
using MySQLStudio.Common;
using MySQLStudio.Common.Data;

namespace MySQLStudio.BusinessLogic
{
    public class DatabaseController
    {
        /// <summary>
        /// Retrieves list of database on host
        /// </summary>
        /// <returns></returns>
        public static List<Database> GetDatabases(string connectionString)
        {
            IDataReader databasesReader = null;
            List<Database> lstDatabases = new List<Database>();

            try
            {
                databasesReader = MySqlDataAccess.ExecuteDataReader(connectionString,
                                                                    InformationSchemaHelper.GetDatabasesCommand());

                while (databasesReader.Read())
                {
                    string databaseName = Convert.ToString(databasesReader["SCHEMA_NAME"]);
                    string defaultCollation = Convert.ToString(databasesReader["DEFAULT_COLLATION_NAME"]);

                    lstDatabases.Add(new Database(databaseName, defaultCollation));
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ErrorMessages.InformationSchema_Failed_to_retrive_databases, ex);
            }
            finally
            {
                if (databasesReader != null)
                {
                    if (!databasesReader.IsClosed)
                        databasesReader.Close();
                }
            }

            return lstDatabases;
        }


        public static Database GetDatabase(string connectionString, string databaseName)
        {
            IDataReader databaseReader = null;
            Database database = null;

            try
            {
                Parameter databaseParam = new Parameter();
                databaseParam.ParameterName = "?databaseName";
                databaseParam.DbType = DbType.String;
                databaseParam.Value = databaseName;

                databaseReader = MySqlDataAccess.ExecuteDataReader(connectionString,
                                                                    InformationSchemaHelper.GetDatabasesCommand(),
                                                                    databaseParam);

                if (databaseReader.Read())
                {
                    database = new Database(Convert.ToString(databaseReader["SCHEMA_NAME"]),
                                            Convert.ToString(databaseReader["DEFAULT_COLLATION_NAME"]));                    
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ErrorMessages.InformationSchema_Failed_to_retrive_databases, ex);
            }
            finally
            {
                if (databaseReader != null)
                {
                    if (!databaseReader.IsClosed)
                        databaseReader.Close();
                }
            }

            return database;
        }
    }
}
